<?php
/**
 * @author : xjw129xjt 531201650@qq.com
 */
namespace app\admin\model;

use think\facade\Cache;
use think\Model;

class User extends Model
{

    public function userInfo()
    {
        return $this->hasOne('UserInfo', 'uid', 'id');
    }

    protected $table = 'user';


    protected $rule = [];

    protected $auto = ['password'];

    public $m_error = '';


    public function setPasswordAttr($value, $data = [])
    {
        return md5(AUTH_KEY . $value); //todo 扩展多种加密方式
    }


    public function getUserInfo($uid)
    {

        $info = Cache::remember("user_{$uid}", function () use ($uid) {
            $info = $this->get($uid);
            $userinfo = $info->userInfo->toArray();
            $info = $info->toArray();
            unset($info['userInfo']);
            return array_merge($info, $userinfo);
        });
        /*        $info = cache("?user_{$uid}", function () use ($uid) {
                    return $this->get($uid)->toArray();
                });*/
        return $info;
    }

    public function changePassword($uid, $password)
    {
        $password = $this->setPasswordAttr($password);
        return $this->where(['uid' => $uid])->update(['password' => $password]);
    }


    public function login($account, $password, $ext = [])
    {
        if (check_is_email($account)) {
            $type = 'email';
        } elseif (check_is_mobile($account)) {
            $type = 'mobile';
        } else {
            $type = 'username';
        }
        $user = $this->where($type, $account)->find();
        if (empty($user) || $user['status'] != 1) {
            $this->m_error = '用户不存在~';
            return false;
        }
        if ($this->setPasswordAttr($password) != $user['password']) {
            $this->m_error = '密码错误~';
            return false;
        }
        return $user['id'];
    }


    public function doLogin($uid)
    {
        $user = $this->getUserInfo($uid);

        $tagids = db('user_tag_link')->where('uid', $uid)->column('tag_id');
        $tag_auth = db('user_tag')->whereIn('id', $tagids)->column('auth');
        $tag_auth = trim(implode(',', $tag_auth), ',');
        $tag_auth = array_unique(explode(',', $tag_auth));
        $auth = [
            'uid' => $user['uid'],
            'username' => $user['username'],
            'nickname' => $user['nickname'],
            'login_time' => time(),
            'auth' => implode(',', $tag_auth),
        ];
        session('user_auth', $auth);
        session('user_auth_sign', md5(implode('|', $auth)));
    }

    public function doLogout()
    {
        session('user_auth', null);
        session('user_auth_sign', null);
    }

    public function resetPassword($password, $uid)
    {

        // $time = time();
        $data = [
            // 'password' => $this->setPasswordAttr($password),
            'password' => $this->setPasswordAttr($password),
            //'update_time' => $time,
        ];
        $rs = $this->where(['uid' => $uid])->update($data);
        return $rs;
    }


}

